package com.mxwl.cbg.common.model.base;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.mxwl.cbg.common.conversion.EpochMillisToLocalDateTimeDeserializer;
import com.mxwl.cbg.common.conversion.LocalDateTimeToEpochMillisSerializer;
import com.mxwl.cbg.common.utils.TimeUtils;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.time.ZonedDateTime;
import java.util.List;
import java.util.Map;

@Data
@Schema(description = "ES对应实体类的基础模型")
public class BaseModel {

    @Schema(description = "服务器id")
    private String serverid;

    @Schema(description = "游戏渠道")
    private String game_channel;

    @Schema(description = "eid")
    private String eid;

    @Schema(description = "定价")
    private Integer price;

    @Schema(description = "类型")
    private Integer equip_type;

    @Schema(description = "等级")
    private Integer equip_level;

    @Schema(description = "类别id")
    private Integer kindid;

    @Schema(description = "名称")
    private String equip_name;

    @Schema(description = "图标")
    private String icon;

    @Schema(description = "是否接受议价")
    private Boolean accept_bargain;

    @Schema(description = "亮点词条")
    private List<LightWord> light_words;


    @Schema(description = "装备sn码")
    private String equip_sn;

    @Schema(description = "装备经验值")
    private Integer sum_exp;

    @Schema(description = "简短描述总结 也是结合装备的高附加值属性的一个描述")
    private String desc_sumup_short;

    @Schema(description = "卖家昵称")
    private String seller_nickname;

    @Schema(description = "卖家角色id")
    private String seller_roleid;

    @Schema(description = "装备数量")
    private Integer equip_count;

    @Schema(description = "描述总结")
    private String desc_sumup;

    @Schema(description = "装备上架版本")
    private Integer equip_onsale_version;

    @Schema(description = "标签，应该是表示这个装备是人的还是宠物的")
    private String tag;

    @Schema(description = "位置编号（应该是装备穿戴的位置）")
    private Integer position;

    @Schema(description = "手续费")
    private Map<String, Integer> poundage_info;

    @Schema(description = "是否有时间锁")
    private Boolean is_time_lock = false;

    @Schema(description = "是否分角色出售")
    private Boolean is_split_sale;
    @Schema(description = "是否是拆分之后的单独出售角色")
    private Boolean is_split_independent_role;
    @Schema(description = "是否是拆分之后打包出售角色")
    private Boolean is_split_main_role;

    @Schema(description = "召唤兽栏位扩展数量")
    private Integer iSumAmountEx;


    @Schema(description = "公开展示的结束时间（也就是公式期），如果说这个时间大于当前，处于公示期")
    @JsonSerialize(using = LocalDateTimeToEpochMillisSerializer.class)
    @JsonDeserialize(using = EpochMillisToLocalDateTimeDeserializer.class)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private ZonedDateTime fair_show_end_time;

    @Schema(description = "销售保护期结束时间")
    @JsonSerialize(using = LocalDateTimeToEpochMillisSerializer.class)
    @JsonDeserialize(using = EpochMillisToLocalDateTimeDeserializer.class)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private ZonedDateTime onsale_protection_end_time;

    @Schema(description = "首次上架的时间")
    @JsonSerialize(using = LocalDateTimeToEpochMillisSerializer.class)
    @JsonDeserialize(using = EpochMillisToLocalDateTimeDeserializer.class)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private ZonedDateTime first_onsale_time;

    @Schema(description = "出售时间，上架的时间")
    @JsonSerialize(using = LocalDateTimeToEpochMillisSerializer.class)
    @JsonDeserialize(using = EpochMillisToLocalDateTimeDeserializer.class)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private ZonedDateTime selling_time;

    @Schema(description = "抽签时间，如果这个时间大于当前，说明处于抽签阶段")
    @JsonSerialize(using = LocalDateTimeToEpochMillisSerializer.class)
    @JsonDeserialize(using = EpochMillisToLocalDateTimeDeserializer.class)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private ZonedDateTime random_draw_finish_time;

    @Schema(description = "结束时间")
    @JsonSerialize(using = LocalDateTimeToEpochMillisSerializer.class)
    @JsonDeserialize(using = EpochMillisToLocalDateTimeDeserializer.class)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private ZonedDateTime expire_time;

    @Schema(description = "结束时间")
    @JsonSerialize(using = LocalDateTimeToEpochMillisSerializer.class)
    @JsonDeserialize(using = EpochMillisToLocalDateTimeDeserializer.class)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private ZonedDateTime create_time;


    public void setBaseData(JSONObject data) {
        //基础数据
        this.setPrice(data.getInteger("price"));
        this.setEid(data.getString("eid"));
        this.setEquip_type(data.getInteger("equip_type"));
        this.setEquip_count(data.getInteger("equip_count"));
        this.setAccept_bargain(data.getBoolean("accept_bargain"));
        this.setEquip_name(data.getString("equip_name"));
        this.setSum_exp(data.getInteger("sum_exp"));
        this.setEquip_sn(data.getString("equip_sn"));
        this.setServerid(data.getString("serverid"));
        this.setSeller_roleid(data.getString("seller_roleid"));
        this.setSeller_nickname(data.getString("seller_nickname"));
        this.setTag(data.getString("tag"));
        this.setPosition(data.getInteger("position"));
        this.setKindid(data.getInteger("kindid"));
        this.setIcon(data.getString("icon"));
        this.setGame_channel(data.getString("game_channel"));
        this.setEquip_onsale_version(data.getInteger("equip_onsale_version"));
        this.setDesc_sumup(data.getString("desc_sumup"));
        this.setDesc_sumup_short(data.getString("desc_sumup_short"));
        this.setEquip_level(data.getInteger("equip_level"));
        //时间相关
        this.setCreate_time(TimeUtils.parseDateTimeWithTimeZone(data.getString("create_time")));
        this.setExpire_time(TimeUtils.parseDateTimeWithTimeZone(data.getString("expire_time")));
        this.setSelling_time(TimeUtils.parseDateTimeWithTimeZone(data.getString("selling_time")));
        this.setFirst_onsale_time(TimeUtils.parseDateTimeWithTimeZone(data.getString("first_onsale_time")));
        this.setFair_show_end_time(TimeUtils.parseDateTimeWithTimeZone(data.getString("fair_show_end_time")));
        this.setOnsale_protection_end_time(TimeUtils.parseDateTimeWithTimeZone(data.getString("onsale_protection_end_time")));
        this.setRandom_draw_finish_time(TimeUtils.parseDateTimeWithTimeZone(data.getString("random_draw_finish_time")));
    }


    /**
     * 亮点词条
     */
    public static class LightWord {
        // 亮点
        private String word;
        // 评分
        private Integer rating;

        public LightWord(Object o, Object o1) {
            this.word = o.toString();
            this.rating = Integer.parseInt(o1.toString());
        }
    }
}
